package com.gxa.www.cl.controller;

import com.gxa.www.cl.common.base.RespResult;
import com.gxa.www.cl.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class SupplierController {

    @Autowired
    private SupplierService supplierService;

    @GetMapping("/suppliers/show")
    public RespResult<List<Map<String, Object>>> getSuppliers() {
        return RespResult.success("成功返回", supplierService.getSuppliers());
    }

    @PostMapping("/suppliers/del")
    public RespResult deleteSupplier(@RequestParam Long id) {
        try {
            supplierService.deleteSupplier(id);
            Map<String, Object> response = new HashMap<>();
            response.put("code", 0);
            response.put("message", "供应商删除成功");
            return RespResult.success(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("code", 1);
            response.put("message", "供应商删除失败");
            return RespResult.error(response.toString());
        }
    }

    @PostMapping("/suppliers/add")
    public RespResult<Map<String, Object>> addSupplier(@RequestBody Map<String, Object> formData) {
        try {
            supplierService.addSupplier(formData); 
            Map<String, Object> response = new HashMap<>();
            response.put("code", 0);
            response.put("message", "供应商添加成功");
            return RespResult.success(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("code", 1);
            response.put("message", "供应商添加失败");
            return RespResult.error(response.toString());
        }
    }
}